const baseUrl = 'https://cnodejs.org/api/v1'

export const http = (url,data,method)=> {
	return new Promise((resolve,reject) => {
		uni.request({
			url: baseUrl + url,
			data,
			method
		})
		.then(data => {
			const [error,res] = data
			if(res.statusCode >= 200&&res.statusCode < 300 || res.statusCode === 304) {
				if(res.data.success) {
					resolve(res.data)
				}else{
					reject(res)
				}
			}else if(res.statusCode === 401){
				// 返回指定字符在字符串中第一次出现处的索引,没有返回-1
				if(getCurrentPages()[getCurrentPages().length-1].route.indexOf('pages/login/login') == -1&&getCurrentPages()[getCurrentPages().length-1].route.indexOf('pages/mine/mine') == -1) {
					uni.navigateTo({
						url:'/pages/login/login'
					})
				}
				reject(res)
			}else{
				reject(res)
			}
		})
	})
}
